<template>
  <div class="department-container">
    <div class="app-container">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <el-row>
            <el-col :span="20">
              <span>{{ componyName }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <el-col>负责人</el-col>
                <el-col>
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="addDept(1)">
                        添加子部门
                      </el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </div>
        <el-tree
          :data="depsList"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row style="width: 100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDept(2, data)">
                          添加子部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="editDepart(data)">
                          编辑部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="delDepart(data)">
                          删除部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <add-departments
      ref="addDep"
      :depslist="depsList"
      :showadddept="showAddDept"
      :currentdep="currentDep"
      @close-dia="closeDia"
    />
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { arrayToTree } from '@/utils'
import AddDepartments from './components/AddDepartments.vue'
export default {
  components: {
    AddDepartments
  },
  data () {
    return {
      currentDep: {},
      showAddDept: false,
      componyName: '',
      depsList: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created () {
    this.getDepatsList()
  },
  methods: {
    async editDepart (data) {
      console.log('编辑部门', data)
      this.showAddDept = true
      this.currentDep = data
      this.$refs.addDep.editDepart(data.id)
    },
    handleNodeClick (data) {
      console.log(data)
    },
    async getDepatsList () {
      const res = await getDepartments()
      this.componyName = res.companyName
      this.depsList = arrayToTree(res.depts)
    },
    addDept (type, data) {
      this.showAddDept = true
      if (type === 2) { this.currentDep = data }
    },
    closeDia () {
      this.showAddDept = false
      this.getDepatsList()
    },
    async delDepart (data) {
      try {
        await this.$confirm('是否删除' + data.name + '?', '提示')

        await delDepartments(data.id)
        this.$message.success('删除成功')
        this.getDepatsList()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-tree-node__content {
  margin: 3px 0;
  font-size: 14px;
}
</style>
